package com.qlm.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.upload.UploadFile;
import com.qlm.common.Define;
import com.qlm.entity.Gift;
import com.qlm.interceptor.AdminOperateInterceptor;
import com.qlm.interceptor.AuthInterceptor;
import com.qlm.service.ICouponService;
import com.qlm.service.IGiftService;
import com.qlm.service.impl.CouponServiceImpl;
import com.qlm.service.impl.GiftServiceImpl;
import com.qlm.view.ResultMessage;

/**
 * 券码管理
 * @author raoy
 *
 */
@Before({AuthInterceptor.class , AdminOperateInterceptor.class})
public class CouponManageController extends Controller {
	
	protected final static Logger logger = LoggerFactory
			.getLogger(CouponManageController.class);	
	private ICouponService couponService = enhance(CouponServiceImpl.class);
	private IGiftService giftService = enhance(GiftServiceImpl.class);
	/**
	 * 跳转券码列表页面
	 */
	public void listCoupon(){
		//获取参数
		String giftID = getPara("id");
		logger.info("method:listCoupon param:giftID(" + giftID + ")");
		Gift gift = giftService.getGiftInfoById(giftID);
		//传递参数
		setAttr("giftId", giftID);
		setAttr("gift", gift);
		render("/page/couponList.jsp");
	}
	
	/**
	 * coupon列表分页
	 */
	public void couponTableSearch(){	
		//获取参数
		String aoData = getPara("aoData");
		String giftID = getPara("giftID");
		logger.info("method:couponTableSearch param:giftID(" + giftID + ")，aoData("+ aoData +")");
		//查询分页信息
		String jsonStr = couponService.findCouponByGiftID(aoData,giftID);
		logger.info("method:couponTableSearch return:jsonStr(" + jsonStr	+ ")");
		renderJson(jsonStr);
	}
	
	/**
	 * 添加券码
	 */
	public void couponAdd(){
		//接收参数
		String giftID = getPara("id");
		String couponname = getPara("couponname");
		String pwd = getPara("pwd");
		logger.info("method:couponAdd param:giftID("+giftID+"),username:("+couponname+")");
		//新增券码
		ResultMessage resultMessage = couponService.saveCoupon(couponname, giftID,pwd);
		logger.info("method:couponAdd return:resultMessage(" + resultMessage	+ ")");
		renderJson("msg",resultMessage);
	}
	
	/**
	 * 删除券码
	 */
	public void couponDel(){
		//接收参数
		String couponIDStr = getPara("id");
		ResultMessage resultMessage = new ResultMessage();
		if(couponIDStr == null){
			resultMessage = ResultMessage.setAllMessage(Define.RM_FAIL, "券码id不能为空");
		}else{
			int couponID = getParaToInt("id");
			String giftID = getPara("gift_id");
			logger.info("method:couponDel param:couponID("+couponID+"),giftID:("+giftID+")");
			//删除券码
			resultMessage = couponService.deleteCoupon(couponID, giftID);
			logger.info("method:couponDel return:resultMessage(" + resultMessage	+ ")");
		}
		renderJson("msg",resultMessage);
	}
	
	/**
	 * 批量导入券码
	 */
	public void couponBatchAdd(){
		//接收参数
		UploadFile couponFile = getFile("fileText");
		String giftID = getPara("id");
		logger.info("method:couponBatchAdd param:giftID("+giftID+")");
		//批量导入券码
		ResultMessage resultMessage = couponService.batchSaveCoupon(couponFile, giftID);
		logger.info("method:couponBatchAdd return:resultMessage(" + resultMessage	+ ")");
		renderJson(resultMessage);
	}
}
